<?php
// 连续队列
set_time_limit(0);
error_reporting(0);

include_once('../lib/config.php');
include_once('../lib/Db.class.php');
include_once('../lib/Model.class.php');
include_once('../lib/Upyun.class.php');
include_once('../lib/Image.class.php');
include_once('../lib/Http.class.php');
include_once('../lib/Snoopy.class.php');
include_once('../lib/Cut.class.php');
include_once('../lib/function.php');

$redis = new Redis();
$redis->connect(REDIS_IP,"6379");  //php客户端设置的ip及端口
if(!$redis) die('redis connect fail');

$redis->auth(REDIS_PASS);

$GLOBALS['config']['master']['db_database'] = 'suucn';
$GLOBALS['config']['slave']['db_database']  = 'suucn';

$pid = getmypid();
$redis->hSet(MH_PIDS, $pid, date('Y-m-d H:i:s').'||gouyg_queue_next');

$model = new Db;
$limit = 500;
$old_max_num = 20;
$old_num = 0;
$site  = 'bookben';

while ($limit--) {
    $stop = $redis->get('LNPAN_QUEUE_STOP');
    if($stop) {
        die('stop');
    }

    if($old_num >= $old_max_num) {
    	$redis->delete('Gouyg_bookben_last_id');
    }

    $last_id = $redis->get('Gouyg_bookben_last_id');
    if(!$last_id) {
	    var_dump($old_num);
	    echo('Gouyg_bookben_last_id');
        sleep(2);
        continue;
    }

    $last_next_id = $last_id - 1;
    $beurl = '/txt/'.$last_next_id.'.shtml';

    var_dump($last_next_id);
    $old = $model->find('soso', "befrom='$site' and beurl='$beurl'");
    if(!$old) {
    	$data = array(
            'befrom' => $site,
            'beurl' => $beurl,
        );
        $model->add('soso', $data);
    }
    else {
    	$old_num++;
    }

    $redis->set('Gouyg_bookben_last_id', $last_next_id);
}

$model->close();
$redis->hDel(MH_PIDS, $pid);

sleep(5);
